﻿using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using Kugar.Core.BaseStruct;
using Kugar.Core.Collections;
using Kugar.Core.ExtMethod;

namespace Kugar.UI.WinForm
{
    [Designer(typeof(RadioButtonExControlDesigner))]
    public partial class RadioButtonEx : RadioButton
    {
        public RadioButtonEx()
        {
            _controlList = new ListEx<Control>();

            this.CheckedChanged += CheckChange;

            _controlList.CollectionChanging += _controlList_CollectionChanging;

            _controlList.CollectionChanged += CheckChange;

            InitializeComponent();

        }

        void _controlList_CollectionChanging(object sender, NotifyCollectionChangingEventArgs<Control> e)
        {
            if (e.Action!=NotifyCollectionChangedAction.Remove)
            {
                e.NewItems.Remove((c) =>
                                      {
                                          return c == this;
                                      });

                if (e.NewItems.Count<=0)
                {
                    e.IsEnable = false;
                }
            }

        }

        public RadioButtonEx(IContainer container):this()
        {
            container.Add(this);

        }


        private ListEx<Control> _controlList;

        [Localizable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(ControlListUITypeDesign), typeof(UITypeEditor))]
        public ListEx<Control> ControlList
        {
            get { return _controlList; }
        }

        private void CheckChange(object sender,EventArgs e)
        {
            if (this.ControlList.Count<=0)
            {
                return;
            }

            foreach (var list in ControlList)
            {
                list.Enabled = this.Checked;
            }
        }
    }
}
